package com.example.demo.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect //告诉框架我是一个切面类
@Component
public class UserAspect {

    /**
     * 切点：配置拦截规则
     */
    @Pointcut("execution(* com.example.demo.controller.UserController.*(..))")
    public void pointcut() {
    }

    /**
     * 前置通知
     */
    @Before("pointcut()")
    public void beforeAdvice() {
        System.out.println("执行了前置通知");
    }

    /**
     * 后置通知
     */
    @After("pointcut()")
    public void aftereAdvice() {
        System.out.println("执行了后置通知");
    }

    /**
     * 环绕通知
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around("pointcut()")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("进入了环绕通知~");
        Object obj = null;
        obj = joinPoint.proceed();
        System.out.println("退出了环绕通知~");
        return obj;
    }

}
